package org.ithang.application.bcAddr.service;

import java.util.ArrayList;
import java.util.List;

import org.ithang.application.bcAddr.bean.BcAddr;
import org.ithang.application.bcAddr.bean.Block;
import org.ithang.application.bcAddr.bean.EthAddr;
import org.ithang.application.bcRecord.service.BcRecordService;
import org.ithang.system.keyvalue.bean.Keyvalue;
import org.ithang.system.keyvalue.service.KeyvalueService;
import org.ithang.tools.database.ModelDao;
import org.ithang.tools.eth.ETHInfo;
import org.ithang.tools.eth.ETHUtil;
import org.ithang.tools.lang.JsonUtils;
import org.ithang.tools.model.Arith;
import org.ithang.tools.redis.JedisUtils;
import org.ithang.tools.redis.Keys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EthAddrService extends ModelDao<EthAddr>{

	@Autowired
	private KeyvalueService keyvalueService;
	
	@Autowired
	private BcRecordService bcRecordService;
	
	@Autowired
	private BcAddrService bcAddrService;
	
	public void process(EthAddr addr){
		listOrders(addr.getAddr());
	}
	
	
	/**
	 * 列出所有交易记录
	 * @param addr
	 * @return
	 */
	public List<Block> listOrders(String addr){
		try{
			Keyvalue kv=keyvalueService.get(Keys.TestDev);
			int mode=0;
			if(null!=kv){
				mode=kv.intValue();
			}
			
			String contractAddress="0xdac17f958d2ee523a2206206994597c13d831ec7";
			if(1==mode){
				contractAddress="0x64b6ea1475203ab4fbba9d72eba3e07a63b2685d";
				System.out.println("在测试链上获取交易记录");
			}else{
				System.out.println("在正试链上获取交易记录");
			}
			
			List<Block> bs=ETHUtil.list(addr,1==mode);
			List<Block> data=new ArrayList<>();
			if(null!=bs&&!bs.isEmpty()){
				for(Block b:bs){
					if("USDT".equalsIgnoreCase(b.getTokenSymbol())&&contractAddress.equalsIgnoreCase(b.getContractAddress())){//如果redis中存在则表明己做过处理
						//doOrder(b.getHash(),b.getTo(),b.getValue());
						data.add(b);
					}else{
						System.out.println("不符合要求：synmbol:"+b.getTokenSymbol()+",contractAddress:"+b.getContractAddress());
					}
				}
			}
			return data;
		}catch(Exception e){
			System.out.println("获取交易记录出错:"+e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 处理交易记录
	 * @param orderStr
	 */
	public void doOrder(String txhash,String to,String value){
		
		try{
			
			int num=keyvalueService.getsInt("select count(0) from bc_record where txhash=?", txhash);
			if(num>0){
				return;
			}
			
			Keyvalue kv=keyvalueService.get(Keys.TestDev);
			int mode=0;
			if(null!=kv){
				mode=kv.intValue();
			}
			System.out.println("查询交易:"+txhash);
			ETHInfo info=ETHUtil.getDetail(txhash, 1==mode);
			
			if(null==info){
				
				info=ETHUtil.detail(txhash);
				if(null!=info&&null!=info.getData()&&!info.getData().isEmpty()){
					info.setResult(info.getData());
				}
			}
			
			
			if(null!=info&&(1==info.getStatus()||200==info.getStatus())){
				System.out.println("查询交易:"+txhash+"成功");
				if(null!=info.getResult()&&!info.getResult().isEmpty()){
					String status=info.getResult().get("status");
					if(null==status||"".equals(status.trim())){
						return;
					}
					
					if("1".equals(status)){
						//成功后做业务处理
						
						kv=keyvalueService.get(Keys.MinRecharge);
						Double rechargeAmount=Arith.div(Double.valueOf(value),1000000,6);
        				if(rechargeAmount<kv.doubleValue()){
        					
        				}else{
        					BcAddr bcAddr=bcAddrService.getByAddr(to);//充值地址匹配
        					if(null!=bcAddr){
        						bcRecordService.rechargeSuccess(JsonUtils.toJsonStr(info), String.valueOf(rechargeAmount),bcAddr.getCustomer_id(),txhash);
        						System.out.println(txhash+"充了"+rechargeAmount);
        					}
        				}
						
					}else{
						//失败后业务处理
						System.out.println("充值失败或未到账");
					}
					
				}
			}else{
				System.out.println("交易:"+txhash+"没有交易结果");
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	
	
}
